package com.hdoit.apollo.web.cargo.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Category;
import com.hdoit.apollo.logical.cargo.service.ICategoryService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.viewmodel.cargo.CategorySearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年03月31日 16:41:57
 * @comment
 */
@Controller
@RequestMapping(value = "/cargo/category/")
public class CategoryController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/cargo/category/";
    }

    private ICategoryService categoryService;
    public ICategoryService getCategoryService() {
        return categoryService;
    }
    @Resource
    public void setCategoryService(ICategoryService categoryService) {
        this.categoryService = categoryService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="detailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String categoryListViewName = "categoryList";
    

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CategorySearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(categoryService.queryCountMP(search));
        List<Category> categorys= categoryService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("categorys", categorys);
        model.addAttribute("categorySearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String categoryDetailed(Model model,String key){
        Category category=new Category();
        if(!StringUtils.isEmpty(key)){
            category=categoryService.queryModel(key);
        }
        model.addAttribute("category", category);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Category category){
        if(StringUtils.isEmpty(category.getCategoryId())){
            category.setCategoryId(CommonUtils.getKeyId());
            category.setCreateDate(DateUtils.getCurrent());
            category.setDeleteMark(YesOrNoInteger.no);
            category.setCreateUserId(getLoginModel().getLoginId());
            category.setCreateUserName(getLoginModel().getLoginName());
            category.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            category.setShopId(shop.getShopId());
        }else{
            category.setModifyDate(DateUtils.getCurrent());
            category.setModifyUserId(getLoginModel().getLoginId());
            category.setModifyUserName(getLoginModel().getLoginName());
            category.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(categoryService.applyChange(category)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("category", category);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value = categoryListViewName, method = { RequestMethod.GET }, produces = "text/html;charset=UTF-8")
   	public @ResponseBody String unitList(PagerModel pagerModel,CategorySearch search) {

    	 List<Category> categories=categoryService.queryListMP(search, null);
   		
   		return JsonUtils.writeValueAsString(categories);
   	}
    
    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(categoryService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}